#include "types.h"
#include "graph.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTEX_SIZE 256
#define MAX_WEIGHT_SIZE 10

bool is_string_float(char name[]);

int read_vertexes(char * nodes_file)
{
	char * line;
	FILE * fin;
	fin = fopen(nodes_file, "r");
	assert(fin != NULL);
	line = (char*)malloc(sizeof(char) * MAX_VERTEX_SIZE);
	assert(line != NULL);
	while (!feof(fin))
	{
		fscanf(fin, "%s\n", line);
		add_vertex(line);
	}
	print();
	fclose(fin);
	free(line);
	return 1;
}

int read_edges(char * edges_file)
{
	FILE * fin;
	double fweight = 0;
	char * vertex1, *vertex2, *weight;
	fin = fopen(edges_file, "r");
	assert(fin != NULL);
	vertex1 = (char*)malloc(sizeof(char) * MAX_VERTEX_SIZE);
	assert(vertex1 != NULL);
	vertex2 = (char*)malloc(sizeof(char) * MAX_VERTEX_SIZE);
	assert(vertex2 != NULL);
	weight = (char*)malloc(sizeof(char) * MAX_WEIGHT_SIZE);
	while (!feof(fin))
	{
		fscanf(fin, "%s %s %s\n", vertex1, vertex2, weight);
		if (is_string_float(weight))
			add_edge_by_name(vertex1, vertex2, atof(weight));
		else
		{ 
			perror("Invalid format of edges file");
			return 0;
		}
	}
	fclose(fin);
	free(vertex1);
	free(vertex2);
	return 1;
}


bool is_string_float(char name[])
{
	int i;
	int counter_point = 0;
	if(name[0]=='\0')
		return false;
    for (i = 0; i < strlen(name) ; i++)
    {
		if (name[i] == '.')
			{
				counter_point++;
				if (counter_point > 1)
					return false;
			}
        else if (name[i] < '0' || name[i] > '9')
        {
			
            return false;
        }
    }
    return true;
}